﻿using System;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Controls;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;

namespace TfsComLib.CheckinDialog
{
    /// <summary>
    ///     Dialog checkin invoker
    /// </summary>
    public sealed class DialogCheckinInv : IDisposable
    {
        private readonly Form dialogCheckin;

        public DialogCheckinInv(Workspace workspace, PendingChange[] selectedChanges, PendingChange[] allChanges,
            string comment, CheckinNote checkinNote, WorkItemCheckedInfo[] checkedInfoList,
            string policyFailureOverrideComment)
        {
            //PendingCheckinManager pendingCheckinManager = WorkspaceContext.Get(base.CurrentWorkspace).PendingCheckinManager;
            var auxAssembly = Assembly.GetAssembly(typeof (ControlAddItemsExclude));
            var workspaceContextType =
                auxAssembly.GetType("Microsoft.TeamFoundation.VersionControl.Controls.WorkspaceContext");
            var mi = workspaceContextType.GetMethod(
                "Get",
                BindingFlags.NonPublic | BindingFlags.Static,
                null,
                new[] {typeof (Workspace)},
                null);

            var workspaceContext = mi.Invoke(null, new object[] {workspace});

            var pendingCheckinManagerType =
                auxAssembly.GetType("Microsoft.TeamFoundation.VersionControl.Controls.PendingCheckinManager");
            var pi = workspaceContextType.GetProperty("PendingCheckinManager",
                BindingFlags.NonPublic | BindingFlags.Instance);
            var pmi = pi.GetGetMethod(true);
            var pendingCheckinManager = pmi.Invoke(workspaceContext, new object[] {});
            //internal DialogCheckinVS(VersionControlServer sourceControl, PendingCheckinManager checkinManager, PendingChange[] checkedChanges, string comment, CheckinNote checkinNote, WorkItemCheckedInfo[] checkedInfoList, string policyFailureOverrideComment);
            //internal DialogCheckinVS(VersionControlServer sourceControl, PendingCheckinManager checkinManager, string[] checkedItems, string comment, CheckinNote checkinNote, WorkItemCheckedInfo[] checkedInfoList, string policyFailureOverrideComment);
            var controlsAssembly = Assembly.GetAssembly(typeof (VersionControlExt));
            var dialogCheckinType =
                controlsAssembly.GetType("Microsoft.VisualStudio.TeamFoundation.VersionControl.DialogCheckinVS");
            var ci = dialogCheckinType.GetConstructor(
                BindingFlags.Instance | BindingFlags.NonPublic,
                null,
                new[]
                {
                    typeof (VersionControlServer), pendingCheckinManagerType, typeof (PendingChange[]), typeof (string),
                    typeof (CheckinNote), typeof (WorkItemCheckedInfo[]), typeof (string)
                },
                null);
            dialogCheckin =
                (Form)
                    ci.Invoke(new[]
                    {
                        workspace.VersionControlServer, pendingCheckinManager, selectedChanges, comment, checkinNote,
                        checkedInfoList, policyFailureOverrideComment
                    });
        }

        public DialogResult DialogResult { get; set; }

        #region IDisposable Members

        public void Dispose()
        {
            if (dialogCheckin != null)
            {
                dialogCheckin.Dispose();
            }
        }

        #endregion

        public void ShowDialog()
        {
            dialogCheckin.ShowDialog();
            DialogResult = dialogCheckin.DialogResult;
        }

        private void Checkin()
        {
            var controlsAssembly = Assembly.GetAssembly(typeof (VersionControlExt));
            var dialogCheckinType =
                controlsAssembly.GetType("Microsoft.VisualStudio.TeamFoundation.VersionControl.DialogCheckinVS");
            var flags = BindingFlags.Instance | BindingFlags.NonPublic;
            var mi = dialogCheckinType.GetMethod(
                "Microsoft.TeamFoundation.VersionControl.Controls.IPendingCheckinHost.Checkin",
                flags,
                null,
                new Type[] {},
                null);
            mi.Invoke(dialogCheckin, new object[] {});
        }
    }
}